package leetcode.other;

import java.io.File;
import java.util.ArrayList;
import java.util.Queue;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

public class PhotoTest {
    static class PhotoManager{
        static ConcurrentHashMap<Integer,File> photoFiles;

        static void putPhotoFile(Integer id,File file){
            photoFiles.put(id,file);
        }

        static int getId(){
            return UUID.randomUUID().variant();
        }
    }
    static class Photo{
        public int id; //图片的id
        public int seq; //图片的序列号 用于抽帧
        public int count; //图片的统计次数
    }

    static class DetectModel{
        public static boolean isFire(File file){
            //使用算法检测 是否含有火灾信息
            //返回的其实是 处理完之后的对象信息
            return true;
        }
    }
    public static ConcurrentHashMap<String, Queue<Photo>> photos;
    public static int isFireCount = 5; //连续出现五帧才判定为火灾事件
    public static void main(String[] args) {
        getPhotos();
        detectFire();
    }
    public static void getPhotos(){
        //从一路摄像头获取图片
        int seq=0; //初始帧为0
        while(true){
            File file = new File("");
            //文件存储到manager中
            seq++;
            if(seq%isFireCount!=0){
                continue;
            }
            Integer id =PhotoManager.getId();
            PhotoManager.putPhotoFile(id,file);
            Photo photo = new Photo();
            Queue<Photo> fire = photos.get("fire");
            fire.offer(photo);
        }
    }

    public static void detectFire(){
        String str="fire";
        Queue<Photo> photoQueue = photos.get(str);
        int fireCount =0; //出现火灾的帧数量
        boolean isAlarm=false; //是否发送报警
        while(!photoQueue.isEmpty()){
            Photo photo = photoQueue.poll();
                    // 根据photoid去获取图片文件本身
                    File file = PhotoManager.photoFiles.get(photo.id);
                    boolean fire = DetectModel.isFire(file);
                    if(fire){ //如果是火灾
                        fireCount++;
                        if(fireCount>=isFireCount&&!isAlarm){
                    isAlarm=true; //设置为true 并且发送报警信息
                    // 报警方法
                }else if(fireCount >= 2*60*25) { //时长大于两分钟这一段视频就结束，开始下一段
                    isAlarm=false;
                    //发送结束报警
                    fireCount=0; //火灾统计归零
                }
            }else{//如果这一帧没有检测出火灾
                if(isAlarm){ //如果已经发送过报警信息了
                    isAlarm=false;
                    //发送结束报警
                    fireCount=0;
                }else{
                    fireCount=0;
                }
            }
        }
    }


}
